﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using Utils.DC.Web.UI;

public partial class Pages_Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["loginClient"] != null)
        {
            Response.Redirect("../Pages/Home.aspx");
        }
        else
        {

        }
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CustomerUI customerUI = new CustomerUI();
        DataTable ds = customerUI.GetAll().Tables[0];
        DataRow drUser = null;
        DataRow drEmail = null;
        try
        {
            drUser = ds.Select("Username='" + txtUsername.Text + "'")[0];
            if (drUser != null)
            {
                lblUser.Visible = true;
            }

        }
        catch (Exception ex)
        {
            try
            {
                lblUser.Visible = false;
                drEmail = ds.Select("Email='" + txtEmail.Text + "'")[0];
                if (drEmail != null)
                {
                    lblEmail.Visible = true;
                }
            }
            catch (Exception ex1)
            {
                lblEmail.Visible = false;
                if (!chkAgree.Checked)
                {
                    lblCheckAgree.Visible = true;
                }
                else
                {
                    lblCheckAgree.Visible = false;

                    string username = txtUsername.Text.Trim();
                    string password = Security.Encrypt(txtRePass.Text.Trim());
                    string fname = null;
                    if (txtFName.Text != "")
                    {
                        fname = txtFName.Text;
                    }
                    string lname = null;
                    if (txtLName.Text != "")
                    {
                        lname = txtLName.Text;
                    }

                    DateTime? date = null;
                    if (txtDate.SelectedDate == null)
                    {
                        date = DateTime.Now;
                    }
                    else
                    {
                        date = txtDate.SelectedDate;
                    }

                    bool gender=false;
                    if (radMale.Checked)
                    {
                        gender = true;
                    }
                    else if(RadFemale.Checked)
                    {
                        gender = false;
                    }
                    string phone = null;
                    if (txtPhone.Text != "")
                    {
                        phone = txtPhone.Text.Trim();
                    }
                    string address = null;
                    if (txtAddress.Text != "")
                    {
                        address = txtAddress.Text.Trim();
                    }

                    string email = txtEmail.Text.Trim();

                    int result = customerUI.Add(username, password, fname, lname, date, gender, phone, address, email, true);

                    if (result > 0)
                    {
                        Response.Write("<script>alert('Register successful'); window.location='../Pages/Login.aspx';</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('Register Fails');</script>");
                    }
                }

            }
        }

    }
}
